<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="queryForm" class="demo-form-inline" ref="queryForm">
          <el-form-item label="手机号">
            <el-input v-model="queryForm.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label="注册时间">
            <el-date-picker v-model="time" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="query">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="userList" style="width: 100%" border v-loading="isLoading">
        <el-table-column label="用户ID" prop="id">
        </el-table-column>
        <el-table-column prop="name" label="用户名">
        </el-table-column>
        <el-table-column prop="portrait" label="头像" align="center">
          <template slot-scope="scope">
            <!-- <img :src=" " alt=""> -->
            <el-avatar :src="scope.row.portrait || require('../../../assets/header.jpeg')"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号">
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" :formatter="formatDate">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <!-- <img :src=" " alt=""> -->
            <i class="status" :class="scope.row.status === 'DISABLE' ? 'status-danger' : 'status-success'"></i>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-if="scope.row.status !== 'DISABLE'">禁用</el-button>
            <el-button size="mini" type="text" @click="handleSelectRole(scope.row)">分配角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="分配角色" :visible.sync="dialogVisible" width="30%">
        <el-select v-model="selectdRoles" multiple placeholder="请选择">
          <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAllocRole">确 定</el-button>
        </div>
      </el-dialog>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryForm.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" style="padding: 10px;" :disabled="isLoading">
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import {
  getAllUser
} from '@/services/user'
import { getAllRole, allocateUserRole, getRolesByUserId } from '@/services/role'
import {
  formatDate
} from '@/utils/formatDate'
export default Vue.extend({
  name: 'UserList',
  data () {
    return {
      queryForm: {
        phone: '',
        startCreateTime: '',
        endCreateTime: '',
        currentPage: 1,
        pageSize: 10
      },
      time: [],
      userList: [],
      isLoading: true,
      total: 0,
      dialogVisible: false,
      roles: [],
      selectdRoles: [],
      currentUser: null
    }
  },
  created () {
    this.loadUser()
  },
  methods: {
    query () {
      this.loadUser()
    },
    async loadUser () {
      this.isLoading = true
      if (this.time.length > 0) {
        this.queryForm.startCreateTime = this.time[0]
        this.queryForm.endCreateTime = this.time[1]
      }
      const {
        data
      } = await getAllUser(this.queryForm)
      this.userList = data.data.records
      this.total = data.data.total
      this.isLoading = false
    },
    formatDate,
    handleSizeChange () {
      //
    },
    handleCurrentChange () {
      //
    },
    handleClose () {
      this.dialogVisible = false
    },
    async handleSelectRole (role: any) {
      this.currentUser = role
      const { data } = await getAllRole()
      this.roles = data.data
      const { data: { data: userRoles } } = await getRolesByUserId((role as any).id)
      this.selectdRoles = userRoles.map((item: any) => item.id)
      this.dialogVisible = true
    },
    async handleAllocRole () {
      await allocateUserRole({
        userId: (this.currentUser as any).id,
        roleIdList: this.selectdRoles
      })
      this.$message.success('操作成功')
      this.dialogVisible = false
    }
  }
})

</script>

<style lang="scss" scope>
</style>
